<!-- prompt -->
<template>
	<view class="prompt-box" :hidden="isHidden">
		<view class="prompt-content">
			<view class="prompt-title">{{title}}</view>
			<input class="prompt-input" :class="{'error':error}" type="text" v-model="text" placeholder="输入内容" />
			<view class="prompt-btn-group d-flex jc-between ai-center">
				<button class="btn-item prompt-cancel-btn" @tap="_cancel">{{btn_cancel}}</button>
				<button class="btn-item prompt-certain-btn" @tap="_confirm">{{btn_certain}}</button>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				isHidden: true,
				type: "",
				text: "",
				error: false
			};
		},
		props: {
			title: {
				type: String,
				default: '修改资料'
			},
			btn_cancel: {
				type: String,
				default: '返回'
			},
			btn_certain: {
				type: String,
				default: '确定'
			}
		},
		methods: {
			hide() {
				this.text = ""
				this.isHidden = true;
			},
			show() {
				this.isHidden = false;
			},
			_cancel() {
				this.text = '';
				this.hide();
				this.$emit('cancel');
			},
			_confirm() {
				if (this.text && !this.text.trim()) {
					this.error = true
				} else {
					this.error = false
					let obj = {
						type: this.type,
						text: this.text
					}
					this.$emit('confirm', obj);
					this.text = '';
				}
			}
		},
		watch: {
			text(newValue) {
				if (newValue.trim()) {
					this.error = false
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.prompt-box {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, .3);

		.prompt-content {
			position: absolute;
			left: 50%;
			top: 50%;
			transform: translate(-50%, -50%);
			background-color: #FFFFFF;
			width: 500upx;
			height: 300upx;

			.prompt-title {
				text-align: center;
				font-size: 34upx;
				padding: 10upx 0;
				color: #333333;
				background-color: #DDDDDD;
			}

			.prompt-input {
				margin: 20upx;
				padding: 4upx 20upx;
				height: 60upx;
				border: 1upx solid #91a580;
				background-color: #FFFFFF;
				border-radius: 12upx;

				&.error {
					border: 1upx solid #ff0000;
				}
			}

			.prompt-btn-group {
				margin-top: 30upx;
				padding: 0 40upx;

				.btn-item {
					padding: 0;
					text-align: center;
					width: 150upx;
					height: 60upx;
					line-height: 60upx;
					font-size: 30upx;

					&.prompt-cancel-btn {
						background-color: #aaff7f;
					}

					&.prompt-certain-btn {
						background-color: #ffaa00;
					}
				}
			}
		}
	}
</style>
